library(randomForest)
library(gbm)
library(e1071)
library(imputation)
library(mlbench)
library(ada)
library(adabag)
library(rpart)


trainData = read.csv("train.csv", sep=' ', header=FALSE)
trainData$V3[trainData$V3 == "NaN"] = NA
trainData = kNNImpute(trainData, k = 4, verbose=F)$x

testData = read.csv("test.csv", sep=' ', header=FALSE)
testData$V3[testData$V3 == "NaN"] = NA
testData$V9[testData$V9 == "NaN"] = NA
testData = kNNImpute(testData, k = 4, verbose=F)$x

luckyData = read.csv("submission9.csv", header=TRUE)

for (i in c(3,9))
{
  trainData[, i] <- trainData[, i] / (max(trainData[, i]) - min(trainData[, i]))
  testData[, i] <- testData[, i] / (max(testData[, i]) - min(testData[, i]))
}

#split <- runif(dim(trainData)[1]) > 0.5  
#train <- trainData[split,]
#test <- trainData[!split,]
train <- trainData[complete.cases(trainData),]
test <- testData[complete.cases(testData),]
names(train) = c("V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11", "V12", "class")
names(test) = c("V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11", "V12")
#colnames(train) = c("V1", "V2", "V3", "class")
#colnames(test) = c("V1", "V2", "V3", "class")
print("start")
gbm1 <- gbm(class ~ . -V7 -V11 -V1 -V4 -V5 -V2 -V12, data=train, distribution="bernoulli", n.trees=800, interaction.depth=5, shrinkage=0.02, bag.fraction=0.65)

rf <- randomForest(as.factor(class) ~ . -V4 -V2 -V5 -V1, train, n.tree=10000)

svm1 <- svm(as.factor(class) ~ ., data=train, kernel="polynomial", degree=5)

train$class <- as.factor(train$class)
ada <- boosting(class ~ ., data=train, mfinal=4)

predict_rf <- predict(rf, test, type="response")
write.csv(predict_rf, "predictions_rf.txt")
predictions_rf <- read.csv("predictions_rf.txt")

predict_gbt <- predict(gbm1, test, n.trees=800, type="response")
predictions_gbt <- round(predict_gbt)

predict_svm <- predict(svm1, test)
write.csv(predict_svm, "predictions_svm.txt")
predictions_svm <- read.csv("predictions_svm.txt")

#test$class <- as.factor(rep(0, nrow(test)))
#test[, "class"] <- as.factor(test[, "class"])
#predict_ada <- predict(ada, test)
write.csv(predict_ada$class, "predictions_ada.txt")
predictions_ada <- read.csv("predictions_ada.txt")

all_predictions <- c(nrow(test))
sum <- predictions_rf["x"] + 2*predictions_gbt + predictions_svm["x"] + 2*luckyData["Category"]
count <- 0
for (i in 1:nrow(test))
{
  if (sum[i, "x"] <3)
  {
    all_predictions[i] <- 0
  } 
  else
  {
    if (sum[i, "x"] == 3) 
    {
      all_predictions[i] <- predictions_rf[i,"x"]
    }
    else
    {
      all_predictions[i] <- 1
    }
  }
  
  if (i %in% c(413))
  {
    print("")
  }
  
  if (all_predictions[i] != luckyData[i, "Category"])
  {
    print(i)
  }
}

#a = table(test[, "class"], all_predictions)
#print(paste("RSS = ",sum(diag(a))/sum(a)))
write.csv(all_predictions, file="submission.txt")
